package immutablecollections.textboxes;

import java.util.*;

public class LeftRightBox extends TextBox
{
     final private  List<TextBox> boxes;

     public static LeftRightBox with(TextBox... boxes)
     {
          return LeftRightBox.withAll(Arrays.asList(boxes));
     }

     public static LeftRightBox withAll(Collection<TextBox> boxes)
     {
          int w = 0;
          int h = 0;
          for (TextBox b : boxes)
          {
               h = Math.max(h, b.getHeight());
               w = w + b.getWidth();
          }
          return new LeftRightBox(w, h, boxes);
     }

     private LeftRightBox(int w, int h, Collection<TextBox> boxes)
     {
          super(w, h);
          this.boxes = new ArrayList<TextBox>(boxes);
     }

     public String getLine(int index)
     {
          StringBuilder sb = new StringBuilder();
          for (TextBox b : boxes)
          {
               sb.append(b.getLine(index));
          }
          return sb.toString();
     }
}
